<template>
  <div id="advert" class="container">
    <h3 class="title">
      <span />
      SEO分析诊断
    </h3>
    <el-button
      type="primary"
      class="add"
      size="small"
      icon="el-icon-plus"
      style="margin-bottom: 15px"
      @click="goAdd"
    >
      开始诊断
    </el-button>
    <!-- <el-button type="danger" size="small" icon="el-icon-delete" @click="Batchdelete">批量删除</el-button> -->
    <BouncedDialog
      :title="'开始检测'"
      :dialog-show.sync="dialogShow"
      width="50%"
      class="advert"
    >
      <el-form
        ref="addForm"
        :model="addForm"
        :rules="rules"
        inline
        class="addForm"
        label-width="100px"
      >
        <el-form-item label="检测站点" prop="type">
          <el-select
            v-model="addForm.website_url"
            placeholder="请选择"
            clearable
            @change="changeSite"
          >
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.domain"
              :value="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="语言" prop="status">
          <el-select v-model="addForm.lang" placeholder="请选择">
            <el-option
              v-for="item in langList"
              :key="item.lang"
              :label="item.lang"
              :value="item.lang"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="模板" prop="status">
          <el-select v-model="addForm.theme" placeholder="请选择">
            <el-option
              v-for="item in langList"
              :key="item.theme"
              :label="item.theme"
              :value="item.theme"
            />
          </el-select>
        </el-form-item>
        <div class="btn">
          <el-button @click="dialogShow = false">取 消</el-button>
          <el-button type="primary" @click="submit()">确 定</el-button>
        </div>
      </el-form>
    </BouncedDialog>
    <el-table
      :data="orderData"
      border
      stripe
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" /> -->
      <el-table-column label="ID" prop="id" width="80" />
      <el-table-column label="站点地址" prop="website_url" width="120px" />
      <el-table-column label="语言" prop="lang" />
      <el-table-column label="模板" prop="theme" />
      <el-table-column label="状态" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 1">检测中</span>
          <span v-if="scope.row.status == 2">已完成</span>
        </template>
      </el-table-column>
      <el-table-column label="检测时间" prop="create_time" />
      <el-table-column label="操作" width="150px" fixed="right">
        <template slot-scope="scope">
          <!-- 新页面打开 -->
          <el-button class="edit" size="mini" @click="report(scope.row)">
            报告
          </el-button>
          <el-button size="mini" class="dele" @click="del(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page.currentPage"
      :page-sizes="page.pageSizes"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
      :hide-on-single-page="page.total < page.size ? true : false"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>
<script>
import seoCheckApi from '@/api/market/seoCheck.js'
import websiteApi from '@/api/website.js'
import { getToken } from '@/utils/auth'
var token = 'Bearer ' + getToken()
export default {
  data() {
    return {
      dialogShow2: false,
      imageUrl: '',
      videoUrl: '',
      videoName: '',
      orderData: [], //
      dialogShow: false,
      options: [], // 站点下拉选项
      langList: [],
      addForm: {
        website_id: '',
        website_url: '',
        lang: '',
        theme: ''
      },
      rules: {
        website_id: [
          { required: 'true', message: '请选择站点', trigger: 'change' }
        ],
        lang: [{ required: 'true', message: '请选择语言', trigger: 'change' }],
        theme: [{ required: 'true', message: '请选择主题', trigger: 'change' }]
      },
      searchForm: {
        title: ''
      },
      page: {
        total: 0,
        currentPage: 1,
        size: 10,
        pageSizes: [10, 20, 30, 40]
      }, // 分页器
      val: {},
      id: '',
      loading: true,
      attachmentType: '', // 附件分类
      uploadType: '' // 上传类型
    }
  },
  created() {
    this.getList()
    this.getWebsiteList()
  },
  mounted() {
    // this.getTikets()
  },
  updated() {},
  methods: {
    changeSite(e) {
      this.addForm.website_id = e.id
      this.addForm.website_url = e.domain
      seoCheckApi
        .SeoCheck({ website_id: this.addForm.website_id, flag: 'start' })
        .then(res => {
          if (res.code == 0) {
            this.langList = res.data
            if (this.langList[0]) {
              this.addForm.lang = this.langList[0].lang
              this.addForm.theme = this.langList[0].theme
            }
          } else {
            this.$message.error(res.msg)
          }
        })
    },

    // 全选
    handleSelectionChange(val) {},
    // 批量删除
    Batchdelete() {},
    // 获取站点列表
    getWebsiteList() {
      websiteApi.websiteList().then(res => {
        this.options = res.data
      })
    },
    // 选择站点
    chooseWedsite(val) {
      this.$forceUpdate()
    },
    // 新增
    goAdd() {
      this.addForm = {}
      this.dialogShow = true
    },
    // 查看报告
    report(val) {
      const routeLink = this.$router.push({
        path: '/seoCheck/report',
        query: {
          id: val.id
        }
      })
    },
    submit() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          seoCheckApi.SeoCheckStart({ ...this.addForm }).then(res => {
            if (res.code == 0) {
              this.$message.success(res.msg)
              this.dialogShow = false
              this.getList()
            } else {
              this.$message.error(res.msg)
            }
          })
        } else {
          this.$message.error('请填写必要信息')
        }
      })
    },

    // 删除
    del(row) {
      this.$confirm('此操作将永久删除此报告, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          seoCheckApi.SeoDel({ id: row.id }).then(res => {
            if (res.code == 0) {
              this.$message.success('已删除')
              this.getList()
            } else {
              this.$message.error('删除失败')
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    search(val) {
      this.getList()
    },
    reset() {
      this.searchForm.title = ''
    },
    // 列表
    getList() {
      this.val.page = this.page.currentPage
      this.val.limit = this.page.size
      this.val.title = this.searchForm.title
      seoCheckApi.SeoCheckList(this.val).then(res => {
        this.orderData = res.data
        this.page.total = res.count
      })
    },
    // 分页规格改变
    handleSizeChange(val) {
      this.page.size = val
      this.page.currentPage = 1
      this.getList()
    },
    // 分页点击
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getList()
    }
  }
}
</script>
<style lang="scss">
.attachment .el-dialog {
  .el-dialog__body {
    height: 600px;
  }

  .el-table {
    .attachmentImg {
      width: 60px;
      height: 60px;
    }
  }

  .el-button--primary {
    background-color: #f84311;
    border-color: #f84311;
    border-radius: 20px;
  }
}

#advert {
  // .advert{
  //   .el-select{
  //       width: calc(100% - 100px);
  //   }
  //   .el-date-editor.el-input{
  //     width: calc(100% - 100px);
  //   }
  // }
  .addForm .el-form-item {
    width: 100%;
  }

  .addForm .el-form-item__content {
    width: calc(100% - 100px);
  }

  .el-date-editor.el-input {
    width: 100%;
  }

  .addForm .el-select {
    width: 100%;
  }
}
</style>
<style scoped lang="scss">
.el-table {
  margin-bottom: 20px;
}

.search {
  min-width: 1350px;
  background-color: #fff;
  padding: 35px 30px 20px;
  border-radius: 5px;
  margin-bottom: 20px;
}

.avatar {
  max-width: 200px;
}

.attachment_imgBox {
  .videoName {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    width: 100%;
  }
}
</style>
